package com.lance.answer.gui.answer.gk.answer.lms.batch.task;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.base.Strings;
import com.lance.answer.gui.answer.Util;
import com.lance.answer.gui.answer.gk.answer.lms.batch.dto.LmsWatchDTO;
import com.lance.answer.gui.answer.gk.answer.lms.util.LmsUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

@Slf4j
public class GetUserCourseInfoTask extends Thread {


    private final String driverPath;
    private final LinkedBlockingDeque<LmsWatchDTO> watchDTOS;

    public  GetUserCourseInfoTask(String driverPath, LinkedBlockingDeque<LmsWatchDTO> watchDTOS) {
        this.driverPath = driverPath;
        this.watchDTOS = watchDTOS;
    }

    @SneakyThrows
    @Override
    public void run() {

        while (watchDTOS.size() > 0) {
            LmsWatchDTO lmsWatchDTO = watchDTOS.take();
            String userName = lmsWatchDTO.getUsername();
            String password = lmsWatchDTO.getPassword();
            log.info("任务：{} 剩余 {} 行 用户名：{} 密码：{}",
                    Thread.currentThread().getName(), watchDTOS.size(), userName, password);
            System.err.println("执行任务11111");
            run(driverPath, lmsWatchDTO);
        }

        if (watchDTOS.size() == 0) {
            log.info("课程所有用户均已执行完毕，系统将在20min后结束");
            TimeUnit.MINUTES.sleep(20);
            return;
        }
    }


    public static void run(String driverPath, LmsWatchDTO lmsWatchDTO) throws Exception {



        String userName = lmsWatchDTO.getUsername();
        String pwd = lmsWatchDTO.getPassword();
        Map<Long, String> courseMap = null;

        ChromeDriver driver = Util.getDriver(driverPath);

        try {
            File file = new File("libs//lmsCourseInfo//" + userName + ".json");
         //   if (!file.exists() || Strings.isNullOrEmpty(IOUtils.toString(new FileInputStream(file), StandardCharsets.UTF_8))) {

                String url = "https://menhu.pt.ouchn.cn/site/ouchnPc/index";
                driver.get(url);
                Thread.sleep(3000);

                LmsUtil.login(driver, userName, pwd);
                Thread.sleep(5000);
                courseMap = LmsUtil.getUserCourseInfo(driver);
                FileUtils.write(file, JSONObject.toJSONString(courseMap, SerializerFeature.PrettyFormat), "utf-8");
         //   }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                driver.close();
                Util.switchToFirst(driver);
                driver.quit();
            }catch (Exception e){

            }
        }


    }

}
